<#assign className = model.className>
<#assign classNameLower = className?uncap_first>
<#assign pkPropName = model.pkProp.name>
<#assign pkPropNameUp = model.pkProp.nameFirstUp>

package ${basepackage}.${moduleName}.service.impl;

import java.util.List;
import java.util.Map;
import tso.dao.Page;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import tso.lang.BaseException;
import tso.service.*;
import tso.spring.Sc;
import tso.util.AssertUtil;
import tso.util.TreeUtil;
import tso.service.BaseService;

import ${basepackage}.${moduleName}.dao.I${className}Dao;
import ${basepackage}.${moduleName}.dao.impl.${className}Dao;
import ${basepackage}.${moduleName}.model.${className};
import ${basepackage}.${moduleName}.service.*;

/**
 * ${model.tableAlias} Service接口实现类
 * @author codeGener
 * @version 1.0
 * @created ${now?string("yyyy-MM-dd HH:mm")}
 */
@Service("${moduleName}.${className}Service,${moduleName}.${className}ServiceEx")
public class ${className}Service extends BaseService implements I${className}Service,I${model.className}ServiceEx{
	//dao
	private I${className}Dao ${classNameLower}Dao = (I${className}Dao) Sc.getBean("${moduleName}.${className}Dao");
	<#if (model.subClazzes?size > 0) >
	<#list model.subClazzes as subClazz>
	private I${subClazz.className}Dao ${subClazz.className?uncap_first}Dao = (I${subClazz.className}Dao) Sc.getBean("${moduleName}.${subClazz.className}Dao");	
	</#list>
	</#if>
	
	public ${className}Service()	{
		baseDao = ${classNameLower}Dao; 
	}
	
<#if !model.fatherClazz??>
<#if pageable==true>
	public Page find${className}PageBySearch(Map<String,Object> paramMap,int pageNumber,int pageSize){
		try {
			Page pageData = this.${classNameLower}Dao.find${className}PageBySearch(paramMap,pageNumber, pageSize);
			//TODO 关联取值和赋值
			return pageData;
		} catch (Exception e) {
			throw new BaseException(e, log);
		}
	}
<#else>
	public List find${className}ListBySearch(Map<String,Object> paramMap){
		try {
			List<${className}> datas = this.${classNameLower}Dao.find${className}ListBySearch(paramMap);
			//TODO 关联取值和赋值
			return datas;
		} catch (Exception e) {
			throw new BaseException(e, log);
		}
	}
</#if>
</#if>
	//---------------
	//
	//数据操作
	//
	//--------------
<#if (model.subClazzes?size==0)>
	public void save${className}(${className} ${classNameLower}){
		try {
			//1业务校验(dao写方法失败报异常)throw new BaseException("该XX代码已存在，请重新输入!");
			
			//2
			${classNameLower}Dao.saveObject(${classNameLower});
		} catch (Exception e) {
			throw new BaseException(e, log);
		}
	}
<#else>
	public void save${className}(${className} ${classNameLower}
	<#list model.subClazzes as subClazz>,List<${subClazz.className}> lst${subClazz.className},String[] del${subClazz.className}Ids</#list>){
		try {
			//1业务校验(dao写方法失败报异常)throw new BaseException("该XX代码已存在，请重新输入!");
			String orderId= ${classNameLower}.get${model.pkProp.name?cap_first}();
			boolean isAdd = ValidateUtil.isEmpty(orderId);//判断是否编辑
			
			//1保存主表
			${classNameLower}Dao.saveObject(${classNameLower});
			//新增处理子表
			if(isAdd){
				orderId= ${classNameLower}.get${model.pkProp.name?cap_first}();
				<#list model.subClazzes as subClazz>
				//${subClazz.className}
				if(lst${subClazz.className}!=null){
					for(${subClazz.className} ${subClazz.className?uncap_first}:lst${subClazz.className}){
						${subClazz.className?uncap_first}.set${subClazz.pkProperty.name?cap_first}(null);
						${subClazz.className?uncap_first}.set${subClazz.fkProperty.name?cap_first}(orderId);
						${subClazz.className?uncap_first}Dao.saveObject(${subClazz.className?uncap_first});
					}
				}
			
				</#list>
			//修改
			}else{
				<#list model.subClazzes as subClazz>
				//新增或更新数据
				if(lst${subClazz.className}!=null){
					for(${subClazz.className} ${subClazz.className?uncap_first}:lst${subClazz.className}){
						String detlId = ${subClazz.className?uncap_first}.get${subClazz.pkProperty.name?cap_first}();
						if (ValidateUtil.isEmpty(detlId)) {//如果是新增
							${subClazz.className?uncap_first}.set${subClazz.fkProperty.name?cap_first}(orderId);
							${subClazz.className?uncap_first}.set${subClazz.pkProperty.name?cap_first}(null);
						}else{//更新明细
							//自己判断是否需要重新从数据库load数据在赋值的
						}
						//
						${subClazz.className?uncap_first}Dao.saveObject(${subClazz.className?uncap_first});
					}
				}
				// 删除数据
				if (del${subClazz.className}Ids!=null)) {
				   for (String deleteId : del${subClazz.className}Ids) {// 删除从表的信息
					   ${subClazz.className?uncap_first}Dao.removeObject(deleteId);
				   }
			    }
				
				</#list>
			}
		} catch (Exception e) {
			throw new BaseException(e, log);
		}
	}
</#if>	
	
	public void remove${className}(String[] ids){
		try {
			for(String id:ids){
				//1主表的处理
				${classNameLower}Dao.removeObject(id);
				//2注意明细表的不处理，读取明细表数据的时候需要先过滤主表的可用状态
			}
		} catch (Exception e) {
			throw new BaseException(e, log);
		}
	}
}